package cmd

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"gitlab-sync/gitlabapi"
)

var (
	groupSyncCmd = &cobra.Command{
		Use:   "group",
		Short: "组同步 $ gitlab-sync group -d={all}/{groupname}",
		Long: `组同步命令:

$ gitlab-sync group -d=all  --同步所有组
$ gitlab-sync group -d={groupname} --同步指定组
$ gitlab-sync group -d={groupname} -p={Y/N} --同步指定组且同步组下的所有项目

`,
		Run: func(cmd *cobra.Command, args []string) {
			data := cmd.Flag("d")
			projects := cmd.Flag("projects")
			var groups []gitlabapi.Group
			if data != nil {
				groups = gitlabapi.CreateGroups(data.Value.String())
			} else {
				fmt.Println("请输入： gitlab-sync group -d={all}/{groupname}")
			}
			if groups != nil && len(groups) > 0 && projects != nil {
				p := projects.Value.String()
				if p == "Y" {
					logrus.Println("开始同步组的projects")
					for _, group := range groups {
						projects := gitlabapi.GetProjectsByGroupId(group)
						gitlabapi.CreateProjectsByList(projects, false)
					}
				}
			}
		},
	}
)

func init() {
	groupSyncCmd.Flags().StringP("d", "d", "", "同步类型")
	groupSyncCmd.Flags().StringP("projects", "p", "", "是否包含项目")
	rootCmd.AddCommand(groupSyncCmd)
}
